#!/bin/bash

./remake.sh

files=0

for file in *.cmm
do
	echo "Testing ${file%.cmm}"
	files=$[files + 1]
	
	# test source to intermediate
	../parser ${file%.cmm}.cmm > tmp0.out
	diff -Naur ${file%.cmm}.i1 tmp0.out
	if [ $? -ne 0 ]; then echo 'Failed source to intermediate'; break; fi
	
	# test optimizer
	python ../optimizer.py < ${file%.cmm}.i1 > tmp1.out
	diff -Naur ${file%.cmm}.i tmp1.out
	if [ $? -ne 0 ]; then echo 'Failed optimizer'; break; fi
	
	# test intermediate to assembly
	python ../assembler.py < ${file%.cmm}.i > tmp2.out
	diff -Naur ${file%.cmm}.s tmp2.out
	if [ $? -ne 0 ]; then echo 'Failed intermediate to assembly'; break; fi
	
	# test running assembly
	if [ -e ${file%.cmm}.in ]
	then
		spim -file ${file%.cmm}.s -quiet < ${file%.cmm}.in | tail --lines=+6 > tmp3.out
	else
		spim -file ${file%.cmm}.s -quiet | tail --lines=+6 > tmp3.out
	fi
	
	diff -Naur ${file%.cmm}.p tmp3.out
	if [ $? -ne 0 ]; then echo 'Fail running assembly'; break; fi
	
done

echo "Tested $files files"
